﻿@{
    ViewBag.Title = " 兑礼网控制中心-会员等级管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<style>
    .fitem label {
        display: inline-block;
        margin-top: 10px;
        width: 110px;
    }
</style>
@*<div class="easyui-layout" style="width:100%;height:100%;">*@
<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newhuiyuan()">添加会员等级</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="edithuiyuan()">编辑会员等级</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="delhuiyuan()">删除会员等级</a>
</div>
<table id="dg" style="width:100%;" class="easyui-datagrid" method="POST" url="@Url.Action("GetList", "Member_Ware")" toolbar="#toolbar" pagination="true" rownumbers="true" fitcolumns="true" singleselect="true" autorowheight="true" resizable="true">
    <thead>
        <tr>
            <th field="Memberlevel">会员等级</th>
            <th field="WareText" width="75">可兑商品</th>
            <th field="ID" width="75" formatter="formattererweima">二维码</th>
        </tr>
    </thead>
</table>

@*</div>*@
<div id="dlg" modal="true" class="easyui-dialog" style="width:480px;height:400px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <input name="ID" hidden="hidden" value="0" />
        <input name="Codes" hidden="hidden" />
        <input name="IsActive" hidden="hidden" />
        <div class="fitem" style="width:100%;">
            <label>会员等级</label>
            <input name="Memberlevel" class="easyui-textbox" required="true">
        </div>


        <div class="fitem" style="width:100%;">
            <div class="ftitle">配置可兑换商品</div>
            <div style="float:left">
                <input id="treeUnSelectSearch" style="width: 300px;" placeholder="检索商品" onkeyup="treeSearch(this, 'treeUnSelect');">
                <div class="easyui-panel" title="商品" style="width: 300px; height: 320px; margin-top: 5px;">
                    <ul id="treeUnSelect" class="easyui-tree"></ul>
                </div>
            </div>
            <div style="float:left;width:50px;text-align:center;padding-top:130px;line-height:20px" id="dvOp">
                <a href="#" style="color:black">－》</a>
            </div>
            <div style="float:left">
                <input id="treeSelectSearch" style="width: 300px;" placeholder="检索已选择商品" onkeyup="treeSearch(this, 'treeSelect');">
                <div class="easyui-panel" title="已选择" style="width:300px;height:320px">
                    <ul id="treeSelect" class="easyui-tree" data-options=""></ul>
                </div>
            </div>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconcls="icon-ok" onclick="save()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>

<script type="text/javascript">

    function formattererweima(v) {
        var dom = location.origin;
        var href='/ware/Create2Code?nr='+dom+'/wechat/djindex?djid='+v;
        return '<a target="_blank" href="' + href + '">二维码</a>';
    }

    function newhuiyuan() {
        //新增
        $('#fm').form('clear');//清空表单
        bandTree(0);
        $('#dlg').dialog({
            title: '新增会员等级',
            width: 800,
            height: 550,
            closed: false,
            cache: false,
            modal: true
        }).dialog('center');
    }

    function save() {
        var trees = $('#treeSelect').tree('getRoots');
        if (trees.length == 0) {
            $.messager.alert('错误', '请选择可兑换商品！');
            return;
        }
        var shangpinids = [];
        $(trees).each(function (index, item) {
            shangpinids[index] = item.id;
        });
        shangpinids.join(',');



        $('#fm').form('submit', {
            url: '/Member_Ware/Save?shangpinids=' + shangpinids,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (result.success == true) {
                    $('#dlg').dialog('close');        // 关闭弹出框
                    $('#dg').datagrid('reload');    // 重新加载表数据

                } else {
                    $.messager.alert('错误', '操作失败！');
                }
            }
        });
    }

    function edithuiyuan() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $("#fileList").html("");
            $('#fm').form('clear');
            bandTree(row.ID);
            $('#dlg').dialog({
                title: '修改会员',
                width: 800,
                height: 550,
                closed: false,
                cache: false,
                modal: true
            }).dialog('center');
            $('#fm').form('load', row);//表单赋值
        }
        else {
            $.messager.alert("警告", "请选择一条！");
        }

    }


    function delhuiyuan() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('警告', '你确定要删除当前选中数据?', function (r) {
                if (r) {
                    $.post("@Url.Action("Del","Member_Ware")", { id: row.ID }, function (data) {
                    if (data.success == true) {
                        $('#dg').datagrid('reload');    // 重新加载表数据
                    }
                    else {
                        $.messager.alert('错误', '操作失败！');
                    }
                });
            }
            });
    }
    else {
        $.messager.alert("警告", "请选择一条！");
    }
}


function bandTree(id) {
    @*区域树*@
    $('#treeUnSelect').tree({
        checkbox: false,
        url: '/Member_Ware/GetTreeWare?status=false&id=' + id,
        onClick: function (node) {//单击事件
            $('#treeUnSelect').tree("remove", node.target)
            $("#treeSelect").tree("append", {
                data: [{
                    "text": node.text,
                    "id": node.id,
                }]
            });
        }
    })
    $('#treeSelect').tree({
        checkbox: false,
        url: '/Member_Ware/GetTreeWare?status=true&id=' + id,
        onClick: function (node) {//单击事件
            $('#treeSelect').tree("remove", node.target)
            $("#treeUnSelect").tree("append", {
                data: [{
                    "text": node.text,
                    "id": node.id,
                }]
            });
        }
    })
}

@*树检索*@
function treeSearch(seter, obj) {
    var val = $(seter).val();
    var nodes = $('#' + obj).tree('getRoots');
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].text.indexOf(val) > -1) {
            $('#' + nodes[i].domId).show();
        } else
            $('#' + nodes[i].domId).hide();
    }
}



</script>
